package com.quectel.core.module.ai.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.quectel.constant.global.SystemConstants;
import com.quectel.core.constants.RedisCacheConstants;
import com.quectel.core.module.ai.dao.AiControlPersonDao;
import com.quectel.core.module.ai.dto.AiControlPersonDto;
import com.quectel.core.module.ai.entity.AiControlPersonEntity;
import com.quectel.core.module.ai.service.AiControlPersonService;
import com.quectel.util.common.CopyUtils;
import com.quectel.util.kit.CacheKit;
import com.quectel.util.kit.Snowflake;
import com.quectel.util.mybatis.MyBatisPlusUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @author rananxin
 * @date 2021-11-24 19:13:58
 */
@DubboService
public class AiControlPersonServiceImpl extends ServiceImpl<AiControlPersonDao, AiControlPersonEntity> implements AiControlPersonService {

    private static final Logger LOGGER = LoggerFactory.getLogger(AiControlPersonServiceImpl.class);

    private static final String AI_CONTROL_PERSON_CACHE_BY_ID = RedisCacheConstants.PROJECT_NAME + "AI_CONTROL_PERSON_CACHE_BY_ID:";

    @Autowired
    private AiControlPersonDao aiControlPersonDao;


    @Override
    public void saveBatch(List<AiControlPersonDto> aiControlPersonDtos) {
        List<AiControlPersonEntity> aiControlPersonEntities = CopyUtils.copyList(aiControlPersonDtos, AiControlPersonEntity.class);
        for (AiControlPersonEntity aiControlPersonEntity : aiControlPersonEntities) {
            aiControlPersonEntity.setId(Snowflake.nextId());
        }
        super.saveBatch(aiControlPersonEntities);
    }

    @Override
    public AiControlPersonDto selectCacheById(Long id) {
        return CacheKit.cacheToRedis(
                () -> selectById(id),
                AI_CONTROL_PERSON_CACHE_BY_ID + id,

                SystemConstants.NOT_NULL_CACHE_EXPIRE_SECONDS
        );
    }

    @Override
    public AiControlPersonDto selectById(Long id) {
        AiControlPersonEntity entity = aiControlPersonDao.selectById(id);
        return paddingField(CopyUtils.copyObj(entity, AiControlPersonDto.class));
    }

    @Override
    public List<AiControlPersonDto> queryList(Map<String, Object> params) {
        List<AiControlPersonEntity> list = aiControlPersonDao.queryList(params);
        List<AiControlPersonDto> result = CopyUtils.copyList(list, AiControlPersonDto.class);
        result.forEach(this::paddingField);
        return result;
    }

    private AiControlPersonDto paddingField(AiControlPersonDto dto) {
        if (dto != null) {

        }
        return dto;
    }

    @Override
    public int queryTotal(Map<String, Object> params) {
        return aiControlPersonDao.queryTotal(params);
    }

    @Override
    public Long save(AiControlPersonDto dto) {
        AiControlPersonEntity entity = CopyUtils.copyObj(dto, AiControlPersonEntity.class);
        entity.setId(Snowflake.nextId());
        aiControlPersonDao.insert(entity);
        return entity.getId();
    }

    @Override
    public void updateById(AiControlPersonDto dto) {
        AiControlPersonEntity entity = CopyUtils.copyObj(dto, AiControlPersonEntity.class);
        aiControlPersonDao.updateById(entity);
        CacheKit.invalidRedisCache(AI_CONTROL_PERSON_CACHE_BY_ID + dto.getId());
    }

    @Override
    public void updateAllColumnById(AiControlPersonDto dto) {
        AiControlPersonEntity entity = CopyUtils.copyObj(dto, AiControlPersonEntity.class);


        MyBatisPlusUtils.updateAllColumnById(entity, aiControlPersonDao);

        CacheKit.invalidRedisCache(AI_CONTROL_PERSON_CACHE_BY_ID + dto.getId());
    }

    @Override
    public void deleteById(Long id) {
        aiControlPersonDao.deleteById(id);

        CacheKit.invalidRedisCache(AI_CONTROL_PERSON_CACHE_BY_ID + id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(Long[] ids) {
        for (Long id : ids) {
            deleteById(id);
        }
    }
}
